<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.keywords" placeholder="账号、名称、订单号" clearable></el-input>
      </el-form-item>
      <el-form-item label="起始时间">
				<el-date-picker
          v-model="dataForm.startTime"
          type="datetime"
          value-format = "yyyy-MM-dd HH:mm:ss"
          placeholder="起始时间">
			  </el-date-picker>
			</el-form-item>
			<el-form-item label="结束时间">
				<el-date-picker
          v-model="dataForm.endTime"
          type="datetime"
          value-format = "yyyy-MM-dd HH:mm:ss"
          placeholder="结束时间">
			  </el-date-picker>
			</el-form-item>
      <el-form-item label="支付状态">
				<el-select v-model="dataForm.payStatus" placeholder="支付状态">
					<el-option label="全部" value="" ></el-option>
					<el-option label="未支付" value="0" ></el-option>
					<el-option label="已支付" value="1" ></el-option>
				</el-select>
			</el-form-item>
      <el-form-item label="商品类型">
				<el-select v-model="dataForm.payType" placeholder="商品类型">
					<el-option label="全部" value="" ></el-option>
					<el-option label="普通商品" value="1" ></el-option>
					<el-option label="抵扣券商品" value="2" ></el-option>
					<el-option label="体验券商品" value="2" ></el-option>
          <el-option label="兑换商品" value="3" ></el-option>
				</el-select>
			</el-form-item>
      <el-form-item label="订单状态">
				<el-select v-model="dataForm.orderStatus" placeholder="订单状态">
					<el-option label="全部" value="" ></el-option>
					<el-option label="待确认" value="0" ></el-option>
					<el-option label="商家已确认" value="1" ></el-option>
					<el-option label="交易完成" value="2" ></el-option>
          <el-option label="预约拒绝" value="3" ></el-option>
          <el-option label="预约失败" value="4" ></el-option>
					<el-option label="未预约" value="5" ></el-option>
					<el-option label="已过期" value="6" ></el-option>
          <el-option label="退货订单" value="7" ></el-option>
          <el-option label="配送中" value="8" ></el-option>
					<el-option label="退货申请" value="20" ></el-option>
					<el-option label="商家驳回" value="21" ></el-option>
          <el-option label="退款失败" value="22" ></el-option>
          <el-option label="商家同意" value="23" ></el-option>
					<el-option label="取消订单" value="24" ></el-option>
					<el-option label="订单已完成（未分佣）" value="25" ></el-option>
				</el-select>
			</el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button @click="
          dataForm.keywords = ''
          dataForm.startTime = ''
          dataForm.endTime = ''
          dataForm.payStatus = ''
          dataForm.payType = ''
          dataForm.orderStatus = ''
          getDataList()">重置</el-button>
        <el-button v-if="isAuth('order:shoppingBusinessOrder:businessOrderExcel')" icon="el-icon-download" type="primary" @click="excel()">导出</el-button>
        <!-- <el-button v-if="isAuth('order:shoppingbusinessorder:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('order:shoppingbusinessorder:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
        <el-button v-if="isAuth('order:shoppingBusinessOrder:modifyBatchOrderStatusById')" type="danger" @click="batchHandle()" :disabled="dataListSelections.length <= 0">批量交易完成</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      default-expand-all="true"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-row v-for="(item,index) in props.row.list" :key="index" :gutter="20">
            <span v-if="item !== null">
              <el-col :span="3">
                商家名称：{{item.storeName}}
              </el-col>
              <el-col :span="3">
                买家名：{{item.trueName}}
              </el-col>
              <el-col :span="3">
                商品数量：{{item.goodsCount}}
              </el-col>
              <el-col :span="3">
                商品单价：{{item.goodsPrice}}
              </el-col>
              <el-col :span="3">
                积分数量：{{item.goodsCommission}}
              </el-col>
              <el-col :span="3">
                商品名称：{{item.goodsTitle}}
              </el-col>
              <el-col :span="3">
                <el-button type="text" size="small" @click="addOrUpdateHandle1(item.id)" style="float:right">订单详情</el-button>
              </el-col>
            </span>
          </el-row>
        </template>
      </el-table-column>
      <!-- <el-table-column type="expand">
        <template slot-scope="props">
        <el-form label-position="left" inline class="demo-table-expand">
          <el-table :data="props.row.list">
            <el-table-column>
              <template slot-scope="scope">
                <span>商家名称：{{scope.row.storeName}}</span>
                <span style="margin-left:120px">买家名：{{scope.row.trueName}}</span>
                <span style="margin-left:120px">商品数量：{{scope.row.goodsCount}}</span>
                <span style="margin-left:120px">商品单价：{{scope.row.goodsPrice}}</span>
                <span style="margin-left:120px">积分数量：{{scope.row.goodsCommission}}</span>
                <span style="margin-left:120px">商品名称：{{scope.row.goodsTitle}}</span>
                <el-button type="text" size="small" @click="addOrUpdateHandle1(scope.row.id)" style="float:right">订单详情</el-button>
              </template>
            </el-table-column> -->
            <!-- <el-table-column label="买家名">
              <template slot-scope="scope">
                <span>{{scope.row.trueName}}</span>
              </template>
            </el-table-column>
            <el-table-column label="商品数量">
              <template slot-scope="scope">
                <span>{{scope.row.goodsCount}}</span>
              </template>
            </el-table-column>
            <el-table-column label="商品单价">
              <template slot-scope="scope">
                <span>{{scope.row.goodsPrice}}</span>
              </template>
            </el-table-column>
            <el-table-column label="积分数量">
              <template slot-scope="scope">
                <span>{{scope.row.goodsCommission}}</span>
              </template>
            </el-table-column>
            <el-table-column label="商品名称">
              <template slot-scope="scope">
                <span>{{scope.row.goodsTitle}}</span>
              </template>
            </el-table-column> -->
            <!-- <el-table-column label="操作" fixed="right" header-align="center" align="center">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="addOrUpdateHandle1(scope.row.id)">订单详情</el-button>
              </template>
            </el-table-column> -->
          <!-- </el-table>
        </el-form>
      </template>
      </el-table-column> -->
      <el-table-column
        prop="payNo"
        header-align="center"
        align="center"
        label="订单编号">
      </el-table-column>
      <el-table-column
        prop="payTime"
        header-align="center"
        align="center"
        label="下单时间">
      </el-table-column>
      <!-- <el-table-column
        prop="payTime"
        header-align="center"
        align="center"
        label="完成时间">
      </el-table-column> -->
      <el-table-column
        prop="payUserName"
        header-align="center"
        align="center"
        label="消费者账号">
      </el-table-column>
      <!-- <el-table-column
        prop="payTrueName"
        header-align="center"
        align="center"
        label="消费者名称">
      </el-table-column> -->
      <el-table-column
        prop="payUserType"
        header-align="center"
        align="center"
        label="消费者类型">
      </el-table-column>
      <el-table-column
        prop="payMoney"
        header-align="center"
        align="center"
        label="支付金额">
        <template slot-scope="scope">
          <span v-if="scope.row.payMoney != null">￥ {{scope.row.payMoney}}</span>
          <span v-if="scope.row.payMoney == null">￥ 0</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="integral"
        header-align="center"
        align="center"
        label="赠送积分">
        <template slot-scope="scope">
          <span v-if="scope.row.integral != null && scope.row.integral != 0">-{{scope.row.integral}}</span>
          <span v-if="scope.row.integral === 0">{{scope.row.integral}}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="giveIntegral"
        header-align="center"
        align="center"
        label="消耗积分">
        <template slot-scope="scope">
          <span>{{scope.row.giveIntegral}}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="arriveMoney"
        header-align="center"
        align="center"
        label="实收">
      </el-table-column>
      <!-- <el-table-column
        prop="ecSalt"
        header-align="center"
        align="center"
        label="配送类型">
        <template slot-scope="scope">
          <span>{{scope.row.ecSalt == 0 ? '物流' : (scope.row.ecSalt == 1 ? '自取' : (scope.row.ecSalt == 2 ? '商家配送' : (scope.row.ecSalt == 3 ? '送贝配送' : '')))}}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        prop="distributionType"
        header-align="center"
        align="center"
        label="配送类型">
        <template slot-scope="scope">
          <span>{{scope.row.distributionType == 0 ? '物流' : (scope.row.distributionType == 1 ? '自取' : (scope.row.distributionType == 2 ? '商家配送' : (scope.row.distributionType == 3 ? '送贝配送' : '')))}}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="isPay"
        header-align="center"
        align="center"
        label="支付状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isPay === '未支付'" type="warning"><span style="color: red">{{scope.row.isPay}}</span></el-tag>
          <el-tag v-if="scope.row.isPay === '已支付'" type="warning"><span style="color: green">{{scope.row.isPay}}</span></el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="orderStatus"
        header-align="center"
        align="center"
        label="订单状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.orderStatus !== ''" type="info">{{scope.row.orderStatus}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="shopName"
        header-align="center"
        align="center"
        label="商家账号">
      </el-table-column>
      <el-table-column
        prop="shopTrueName"
        header-align="center"
        align="center"
        label="商家名称">
      </el-table-column>
      <el-table-column
        prop="storeCommission"
        header-align="center"
        align="center"
        label="佣金比">
        <template slot-scope="scope">
          <span v-if="scope.row.storeCommission != null">{{scope.row.storeCommission}}%</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="orderType"
        header-align="center"
        align="center"
        label="订单类型">
      </el-table-column>
      <!-- <el-table-column
        prop="orderStatus"
        header-align="center"
        align="center"
        label="订单状态">
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.orderStatus !== ''">{{scope.row.orderStatus}}</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id,scope.row.orderType,scope.row.payNo)">订单详情</el-button>
          <!-- <el-button type="text" size="small" @click="addOrUpdateHandle1(scope.row.payNo)">子订单信息</el-button> -->
          <!-- <el-button type="text" size="small" @click="deleteHandle(scope.row.orderid)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <add-or-update1 v-if="addOrUpdateVisible1" ref="addOrUpdate1" @refreshDataList="getDataList"></add-or-update1>
  </div>
</template>

<script>
  // import AddOrUpdate from './shoppingbusinessorder-add-or-update'
  import AddOrUpdate from './new-businessorder-details'
  import AddOrUpdate1 from './sonorder'
  export default {
    data () {
      return {
        dataForm: {
          keywords: '',
          startTime: '',
          endTime: '',
          payStatus: '',
          payType: '',
          orderStatus: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        addOrUpdateVisible1: false
      }
    },
    components: {
      AddOrUpdate,
      AddOrUpdate1
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/business/order/queryOrderTreeList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keywords': this.dataForm.keywords,
            'startTime': this.dataForm.startTime,
            'endTime': this.dataForm.endTime,
            'payStatus': this.dataForm.payStatus,
            'payType': this.dataForm.payType,
            'orderStatus': this.dataForm.orderStatus
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id, orderType, payNo) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, orderType, payNo)
        })
      },
      // 到子订单页面
      addOrUpdateHandle1 (payNo) {
        this.addOrUpdateVisible1 = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate1.init(payNo)
        })
      },
      excel () {
        window.location.href = this.$http.adornUrl(`/business/order/businessOrderExcel?token=${this.$cookie.get('token')}&payStatus=${this.dataForm.payStatus}&payType=${this.dataForm.payType}&startTime=${this.dataForm.startTime}&endTime=${this.dataForm.endTime}&keywords=${this.dataForm.keywords}`)
      },
      // 批量交易完成
      batchHandle () {
        var payNos = this.dataListSelections.map(item => {
          return item.payNo
        })
        var shopId = this.dataListSelections.map(item => {
          return item.shopId
        })
        var orderStatus = this.dataListSelections.map(item => {
          return item.orderStatus
        })
        for (var i = 0; i < orderStatus.length; i++) {
          debugger
          if (orderStatus[i] !== '商家已确认') {
            return this.$message.error('只能修改商家已确认的订单为完成！')
          }
        }
        this.$confirm(`确定进行批量修改操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/business/order/modifyBatchOrderStatusById/${shopId}`),
            method: 'post',
            data: this.$http.adornData(payNos, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
